//! # 依赖注入数据模型定义
//! 
//! 本模块定义了依赖注入容器使用的数据模型，包括组件生命周期、状态、优先级和元数据等。

use std::collections::HashMap;
use std::fmt::Debug;

/// 组件生命周期类型
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ComponentLifecycle {
    /// 单例模式 - 整个应用生命周期只有一个实例
    Singleton,
    /// 瞬态模式 - 每次请求都创建新实例
    Transient,
    /// 作用域模式 - 在特定作用域内为单例
    Scoped,
    /// 线程局部模式 - 每个线程一个实例
    ThreadLocal,
}

/// 组件运行状态
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComponentState {
    Uninitialized,
    Initializing,
    Initialized,
    Starting,
    Running,
    Stopping,
    Stopped,
    Error(String),
}

/// 组件优先级
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ComponentPriority(pub i32);

impl Default for ComponentPriority {
    fn default() -> Self { Self(0) }
}

impl ComponentPriority {
    pub fn high() -> Self { Self(-100) }
    pub fn low() -> Self { Self(100) }
    pub fn custom(priority: i32) -> Self { Self(priority) }
}

/// 组件元数据
#[derive(Debug, Clone)]
pub struct ComponentMetadata {
    pub name: String,
    pub description: Option<String>,
    pub version: String,
    pub tags: HashMap<String, String>,
    pub priority: ComponentPriority,
    pub lifecycle: ComponentLifecycle,
    pub enabled: bool,
}

impl Default for ComponentMetadata {
    fn default() -> Self {
        Self {
            name: "unnamed".to_string(),
            description: None,
            version: "1.0.0".to_string(),
            tags: HashMap::new(),
            priority: ComponentPriority::default(),
            lifecycle: ComponentLifecycle::Singleton,
            enabled: true,
        }
    }
}

impl ComponentMetadata {
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into(), ..Default::default() }
    }
    
    pub fn with_lifecycle(mut self, lifecycle: ComponentLifecycle) -> Self {
        self.lifecycle = lifecycle;
        self
    }
    
    pub fn with_priority(mut self, priority: ComponentPriority) -> Self {
        self.priority = priority;
        self
    }
}

/// 内部组件注册信息
#[derive(Debug)]
pub(crate) struct ComponentRegistration {
    pub type_id: std::any::TypeId,
    pub metadata: ComponentMetadata,
    pub factory: Box<dyn std::any::Any + Send + Sync>,
    pub dependencies: Vec<std::any::TypeId>,
    pub state: std::sync::Arc<tokio::sync::RwLock<ComponentState>>,
}

/// 内部组件实例信息
#[derive(Debug)]
pub(crate) struct ComponentInstance {
    pub type_id: std::any::TypeId,
    pub instance: std::sync::Arc<dyn std::any::Any + Send + Sync>,
    pub metadata: ComponentMetadata,
    pub state: std::sync::Arc<tokio::sync::RwLock<ComponentState>>,
    pub created_at: std::time::Instant,
    pub last_accessed: std::sync::Arc<tokio::sync::RwLock<std::time::Instant>>,
}